﻿// -----------------------------------------------------------------------
// <copyright file="TinhThanhData.cs" company="VDC" author="HuongNT">
// Class TinhThanhData
// </copyright>
// -----------------------------------------------------------------------

namespace VDC.VONE.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.Reflection;
    using Microsoft.ApplicationBlocks.Data;
    using VDC.COMMON;
    using VDC.VONE.ENTITY;
    
    /// <summary>
    /// Class TinhThanhData.cs
    /// </summary>
    public class TinhThanhData
    {
        /// <summary>
        /// Gets DS tinh thanh.
        /// </summary>
        /// <returns>Return List Tinh thanh</returns>
        public IList<TinhThanh> GetTinhThanh()
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString();

            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<TinhThanh> lstTinhThanh = new List<TinhThanh>();
                TinhThanh tinhthanh;
                SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneTinhThanhGet);
                while (reader.Read())
                {
                    tinhthanh = new TinhThanh();

                    if (!reader.IsDBNull(0))
                    {
                        tinhthanh.IdTinhThanh = reader.GetInt32(0);
                    }

                    if (reader.GetString(1) != null)
                    {
                        tinhthanh.MaTinhThanh = reader.GetString(1);
                    }

                    if (reader.GetString(2) != null)
                    {
                        tinhthanh.TenTinhThanh = reader.GetString(2);
                    }

                    lstTinhThanh.Add(tinhthanh);
                }

                reader.Close();
                reader.Dispose();

                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstTinhThanh;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// Gets Tinh thanh
        /// </summary>
        /// <param name="idTinhThanh">The id tinh thanh</param>
        /// <returns>return TinhThanh</returns>
        public TinhThanh GetTinhThanhById(int idTinhThanh)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString();

            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                TinhThanh tinhthanh = null;
                SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneTinhThanhGetById, idTinhThanh.ToString());

                while (reader.Read())
                {
                    tinhthanh = new TinhThanh();
                    if (!reader.IsDBNull(0))
                    {
                        tinhthanh.IdTinhThanh = reader.GetInt32(0);
                    }

                    if (reader.GetString(1) != null)
                    {
                        tinhthanh.MaTinhThanh = reader.GetString(1);
                    }

                    if (reader.GetString(2) != null)
                    {
                        tinhthanh.TenTinhThanh = reader.GetString(2);
                    }
                }

                reader.Close();
                reader.Dispose();

                LogHelper.Instance.WriteInfo(Constants.End + methodName);

                return tinhthanh;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// Inserts the specified tinh thanh
        /// </summary>
        /// <param name="tinhthanh">The tinh thanh</param>
        /// <returns>return int</returns>
        public int Insert(TinhThanh tinhthanh)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString();
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParams = new SqlParameter[]
                {
                    new SqlParameter(Constants.SqlParamerterName.IdTinhThanh, tinhthanh.IdTinhThanh), 
                    new SqlParameter(Constants.SqlParamerterName.MaTinhThanh, tinhthanh.MaTinhThanh),
                    new SqlParameter(Constants.SqlParamerterName.TenTinhThanh, tinhthanh.TenTinhThanh)
                };

                int result = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneTinhThanhInsert, sqlParams));

                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return result;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName
                    + Constants.Colon + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// Updates the specified tinh thanh
        /// </summary>
        /// <param name="tinhthanh">The tinh thanh</param>
        public void Update(TinhThanh tinhthanh)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString();
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParams = new SqlParameter[]
                {
                    new SqlParameter(Constants.SqlParamerterName.IdTinhThanh, tinhthanh.IdTinhThanh), 
                    new SqlParameter(Constants.SqlParamerterName.MaTinhThanh, tinhthanh.MaTinhThanh),
                    new SqlParameter(Constants.SqlParamerterName.TenTinhThanh, tinhthanh.TenTinhThanh)
                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneTinhThanhUpdate, sqlParams);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// Xoa tinh thanh
        /// </summary>
        /// <param name="idTinhThanh">The id tinh thanh</param>
        public void Delete(int idTinhThanh)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString();
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParams = new SqlParameter[]
                {
                    new SqlParameter(Constants.SqlParamerterName.IdTinhThanh, idTinhThanh)
                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneTinhThanhDelete, sqlParams);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw;
            }
        }
    }
}
